#include "menumanagerapi.h"

MenuManagerApi::MenuManagerApi(QObject *parent)
    : QObject{parent}
{
    baseUrl = Config::baseUrl;
    manager = new QNetworkAccessManager(0);
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));//post请求设置为json，才能发送josn格式文件
    request.setRawHeader("Authorization",  qApp->property("token").value<QString>().toLocal8Bit());
}

QByteArray *MenuManagerApi::sys_normal_disable()
{
    request.setUrl(baseUrl + "system/dict/data/type/sys_normal_disable");
    QNetworkReply *reply= manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *MenuManagerApi::sys_show_hide()
{
    request.setUrl(baseUrl + "system/dict/data/type/sys_show_hide");
    QNetworkReply *reply= manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *MenuManagerApi::list(QString menuName,QString status)
{
    QString params = "system/menu/list?";
    if(!menuName.isEmpty()){
        params += "menuName=" + menuName;
    }
    if(!status.isEmpty()){
        params += "&status=" + status;
    }
    request.setUrl(baseUrl + params);
    QNetworkReply *reply= manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *MenuManagerApi::menu(Menu &menu)
{
    request.setUrl(baseUrl + "system/menu");
    QJsonObject obj = menu.toJson();
    QByteArray arr = QJsonDocument(obj).toJson();
    QNetworkReply *reply = menu.menuId > 0 ? manager->put(request,arr) : manager->post(request,arr);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *MenuManagerApi::deleteById(qulonglong menuId)
{
    request.setUrl(baseUrl + "system/menu/" + QString::number(menuId));
    QNetworkReply *reply = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *MenuManagerApi::getById(qulonglong menuId)
{
    request.setUrl(baseUrl + "system/menu/" + QString::number(menuId));
    QNetworkReply *reply = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}
